//
//  ZCNetworkManager.m
//  zctx
//
//  Created by ZhengDarren on 15/9/28.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCNetworkManager.h"

@implementation ZCNetworkManager

+ (ZCNetworkManager *)sharedInstance
{
    static ZCNetworkManager *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

// 请求头
-(NSString *)strURLBasic{
    
//    return @"http://test.zhongcetianxia.com";  //测试
    
    return @"http://www.zhongcetianxia.com";  //正式
}


- (NSString *)strURLHeader
{
    NSString *urlStr = [NSString  stringWithFormat:@"%@%@",[self strURLBasic],@"/index.php/Api/V1"];
    return urlStr;
}

//-----------------------------以下为业务代码-------------------------------------

//1.1 请求头

- (void)requestUrlRequestHeaderssuccess:(void (^)(NSString *urlStr))blockSuccess failure:(void (^)(NSError *error))blockFailure{
    
    [super getWithURLString:URL_REQUEST_HEADER
                     params:nil
                    success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         
         
     }
                    failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         blockFailure(error);
     }];

    
}

// 2.1 验证码下发接口

- (void)requestGetVerifyCodeWithAction:(NSString *)strAction
                                mobile:(NSString *)strMobile
                               success:(void (^)(NSDictionary *dictResponse))blockSuccess
                               failure:(void (^)(NSError *error))blockFailure
{
    if (!(strAction && strMobile))
    {
        return;
    }
    
    [super getWithURLString:URL_GET_VERIFY_CODE
                     params:@{@"action":strAction,
                              @"mobile":strMobile}.mutableCopy
                    success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSDictionary *dit = (NSDictionary *)[responseObject objectForKey:@"content"];
         blockSuccess(dit);
     }
                    failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         blockFailure(error);
     }];
}

// 2.2验证码验证接口
- (void)requestVerifyCodeWithCode:(NSString *)strCode
                           mobile:(NSString *)strMobile
                     device_token:(NSString *)strdevice_token
                          success:(void (^)(NSDictionary *dictResponse))blockSuccess
                          failure:(void (^)(NSError *error))blockFailure{
    
    if (!(strCode && strMobile && strdevice_token))
    {
        return;
    }
    
    [super getWithURLString:URL_VERIFY_CODE
                     params:@{@"code":strCode,
                              @"mobile":strMobile,
                              @"device_token":strdevice_token}.mutableCopy
                    success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
//         NSDictionary *dict = responseObject[@"content"];
         blockSuccess(responseObject);
     }
                    failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         blockFailure(error);
     }];

    
}

//2.3 用户刷新个人资料接口

- (void)requestGetMemberInfoWithMid:(NSString *)strMid
                           password:(NSString *)strPassword
                       device_token:(NSString *)strdevice_token
                            success:(void (^)(NSDictionary *dictResponse))blockSuccess
                            failure:(void (^)(NSError *error))blockFailure{
    if (!(strMid && strPassword && strdevice_token))
    {
        return;
    }
    
    [super getWithURLString:URL_GET_MEMBER_INFO
                     params:@{@"mid":strMid,
                              @"password":strPassword,
                              @"device_token":strdevice_token}.mutableCopy
                    success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         blockSuccess(responseObject);
     }
                    failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         blockFailure(error);
     }];
    
}


//2.3.1 微信登陆接口
- (void)requestGetMemberInfobyunionidWithUnionid:(NSString *)strUnionid
                                          openid:(NSString *)strOpenid
                                       from_type:(NSString *)strfromType
                                    device_token:(NSString *)strdevice_token
                                         success:(void (^)(NSDictionary *dictResponse))blockSuccess
                                         failure:(void (^)(NSError *error))blockFailure{
    if (!(strUnionid && strOpenid && strfromType && strdevice_token))
    {
        return;
    }
    
    NSMutableDictionary *mDictParams = [NSMutableDictionary new];
    [mDictParams setObject:strUnionid forKey:@"unionid"];
    [mDictParams setObject:strOpenid forKey:@"openid"];
    [mDictParams setObject:strfromType forKey:@"from_type"];
    [mDictParams setObject:strdevice_token forKey:@"device_token"];
    
    [super getWithURLString:URL_GET_MEMBER_INFO_BY_UNIONID
                     params:mDictParams
                    success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSDictionary *dict =(NSDictionary *) responseObject[@"content"];
         blockSuccess(dict);
     }
                    failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         blockFailure(error);
     }];
    
}



//2.4 获取分享到微信朋友圈的信息

- (void)requestGetMyshareWithMid:(NSString *)strmid
                        password:(NSString *)strpassword
                         success:(void (^)(NSDictionary *dictResponse))blockSuccess
                         failure:(void (^)(NSError *error))blockFailure{
    if (!(strmid && strpassword))
    {
        return;
    }
    
    [super getWithURLString:URL_GET_MY_SHARE
                     params:@{@"mid":strmid,
                              @"password":strpassword}.mutableCopy
                    success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSDictionary *dict = (NSDictionary *)[responseObject objectForKey:@"content"];
         blockSuccess(dict);
     }
                    failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         blockFailure(error);
     }];
    
}
// 2.5获取我的通知
- (void)requestGetMyNoticeWithMid:(NSString *)strmid password:(NSString *)strpassword success:(void (^)(NSArray *))blockSuccess failure:(void (^)(NSError *))blockFailure{
    
    if (!(strmid && strpassword))
    {
        return;
    }
    [super getWithURLString:URL_GET_MY_NOTICE
                     params:@{@"mid":strmid,
                              @"password":strpassword}.mutableCopy
                    success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSArray *dict = (NSArray *)[responseObject objectForKey:@"content"];
         blockSuccess(dict);
     }
                    failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         blockFailure(error);
     }];
}


//3.1 获取套餐列表
- (void)requestFoundingGetPackagessuccess:(void (^)(NSArray *array))blockSuccess failure:(void (^)(NSError *error))blockFailure{

    [super getWithURLString:URL_FOUNDING_GET_PACKAGES
                     params:nil
                    success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSArray *array = [responseObject dz_modelsForArrayKey:@"content"
                                                modelClassName:@"ZCItemModel" except:nil block:nil];
         blockSuccess(array);
         
     }
                    failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         blockFailure(error);
     }];
    
}

// 3.1.1 获取检测套餐详情
- (void)requestFoundingGetPackageWithId:(NSString *)strId
                                success:(void (^)(ZCItemModel *model))blockSuccess
                                failure:(void (^)(NSError *error))blockFailure {
    if (!strId) {
        return;
    }
    [super getWithURLString:URL_FOUNDING_GET_PACKAGES_INFORMATION
                     params:@{@"id":strId}.mutableCopy
                    success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         ZCItemModel *model = [[ZCItemModel alloc] init];
         model.content = responseObject[@"content"];
         model.information = responseObject[@"information"];
         model.name = responseObject[@"name"];
         blockSuccess(model);
     }
                    failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         blockFailure(error);
     }];
}


//3.2 获取第三方检测机构列表
- (void)requestFoundingGetLabssuccess:(void (^)(NSArray *array))blockSuccess
                              failure:(void (^)(NSError *error))blockFailure{
    
    
    [super getWithURLString:URL_FOUNDING_GET_LABS
                     params:nil
                    success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSArray *array = [responseObject dz_modelsForArrayKey:@"content"
                                                modelClassName:@"ZCMechanismModel" except:nil block:nil];
         blockSuccess(array);
     }
                    failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         blockFailure(error);
     }];
    
}

//3.3 发布众筹提交后台审核
- (void)requestFoundingPublishWithMid:(NSString *)strmid
                             password:(NSString *)strpassword
                               pjname:(NSString *)strpjname
                               pdname:(NSString *)strpdname
                             citycode:(NSString *)strcitycode
                                brand:(NSString *)strbrand
                            packageid:(NSString *)strpackageid
                        institutionid:(NSString *)instiutionid
                               emoney:(NSString *)stremoney
                             fromtype:(NSString *)strfromtype
                              content:(NSString *)strcontent
                              success:(void (^)(NSDictionary *dictResponse))blockSuccess
                              failure:(void (^)(NSError *error))blockFailure{
    if (!(strmid && strpassword && strpjname && strpdname && strcitycode && strbrand && strpackageid && instiutionid && stremoney && strfromtype))
    {
        return;
    }
    
    NSMutableDictionary *mDictParams = [NSMutableDictionary new];
    [mDictParams setObject:strmid forKey:@"mid"];
    [mDictParams setObject:strpassword forKey:@"password"];
    [mDictParams setObject:strpjname forKey:@"pjname"];
    [mDictParams setObject:strpdname forKey:@"pdname"];
    [mDictParams setObject:strcitycode forKey:@"citycode"];
    [mDictParams setObject:strbrand forKey:@"brand"];
    [mDictParams setObject:strpackageid forKey:@"packageid"];
    [mDictParams setObject:instiutionid forKey:@"institutionid"];
    [mDictParams setObject:stremoney forKey:@"emoney"];
    [mDictParams setObject:strfromtype forKey:@"fromtype"];
    [mDictParams setObject:strcontent forKey:@"content"];
    

    [super postWithURLString:URL_FOUNDING_PUBLISH
                       params:mDictParams
                      success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         blockSuccess(responseObject);
     }
                    failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         blockFailure(error);
     }];
    
}

//3.4 获取众筹列表(包括搜索功能)

- (void)requestFoundingGetAllWithType:(NSString *)strtype
                                  mid:(NSString *)strmid
                             password:(NSString *)strpassword
                                mtype:(NSString *)strmtype
                            pagecount:(NSString *)strpagecount
                             keywords:(NSString *)strkeywords
                              success:(void (^)(NSArray *array))blockSuccess
                              failure:(void (^)(NSError *error))blockFailure{
    if (!(strtype && strmid && strpassword && strmtype && strpagecount && strkeywords))
    {
        return;
    }
    
    NSMutableDictionary *mDictParams = [NSMutableDictionary new];
    [mDictParams setObject:strtype forKey:@"type"];
    [mDictParams setObject:strmid forKey:@"mid"];
    [mDictParams setObject:strpassword forKey:@"password"];
    [mDictParams setObject:strmtype forKey:@"mtype"];
    [mDictParams setObject:strpagecount forKey:@"pagecount"];
    [mDictParams setObject:strkeywords forKey:@"keywords"];
    
    [super getWithURLString:URL_FOUNDING_GET_ALL
                     params:mDictParams
                    success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         
//     NSArray *array = [responseObject dz_modelsForArrayKey:@"content"                                               modelClassName:@"ZCMainModel" except:nil block:nil];
         
         blockSuccess([responseObject objectForKey:@"content"]);
     }
                    failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         blockFailure(error);
     }];
    
}


//3.4.1 获取众筹列表(首页最新)

- (void)requestFoundingGetLatestWithMid:(NSString *)strmid
                               password:(NSString *)strpassword
                                success:(void (^)(NSDictionary *dictResponse))blockSuccess
                                failure:(void (^)(NSError *error))blockFailure{
    if (!(strmid && strpassword))
    {
        return;
    }
    [super getWithURLString:URL_FOUNDING_GET_LATEST
                     params:@{@"mid":strmid,
                              @"password":strpassword}.mutableCopy
                    success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         blockSuccess(responseObject);
     }
                    failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         blockFailure(error);
     }];
    
}


//3.5 获取单个众筹项目详情
- (void)requestFoundingGetOneWithId:(NSString *)strid
                                Mid:(NSString *)strmid
                           password:(NSString *)strpassword
                            success:(void (^)(NSDictionary *dictResponse))blockSuccess
                            failure:(void (^)(NSError *error))blockFailure{
    if (!(strid && strmid && strpassword))
    {
        return;
    }
    
    [super getWithURLString:URL_FOUNDING_GET_One
                     params:@{@"id":strid,
                              @"mid":strmid,
                              @"password":strpassword}.mutableCopy
                    success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSDictionary *dict = responseObject[@"content"];
         blockSuccess(dict);
     }
                    failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         blockFailure(error);
     }];
    
}


//3.5.1 获取单个众筹项目赞助列表

- (void)requestFoundingGetOneSponsorWithId:(NSString *) strid
                                       Mid:(NSString *)strmid
                                  password:(NSString *)strpassword
                                 pagecount:(NSString *)strpagecount
                                   success:(void (^)(NSDictionary *dictResponse))blockSuccess
                                   failure:(void (^)(NSError *error))blockFailure{
    if (!(strid && strmid && strpassword && strpagecount))
    {
        return;
    }
    
    [super getWithURLString:URL_FOUNDING_GET_One_SPONSOR
                     params:@{@"id":strid,
                              @"mid":strmid,
                              @"password":strpassword,
                              @"pagecount":strpagecount}.mutableCopy
                    success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         blockSuccess(responseObject);
     }
                    failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         blockFailure(error);
     }];
    
}

// 3.6 发起众筹接口
- (void)requestFoundingPublishsWithMid:(NSInteger)strmid
                             password:(NSString *)strpassword
                               pjname:(NSString *)strpjname
                               pdname:(NSString *)strpdname
                             citycode:(NSString *)strcitycode
                                brand:(NSString *)strbrand
                            packageid:(NSString *)strpackageid
                        institutionid:(NSString *)instiutionid
                               emoney:(NSString *)stremoney
                              success:(void (^)(NSDictionary *dictResponse))blockSuccess
                              failure:(void (^)(NSError *error))blockFailure{
    if (!(strmid && strpassword && strpjname && strpdname && strcitycode && strbrand && strpackageid && instiutionid && stremoney))
    {
        return;
    }
    
    [super postWithURLString:URL_FOUNDING_PUBLISH
                      params:@{@"mid":@(strmid),
                               @"password":strpassword,
                               @"pjname":strpjname,
                               @"pdname":strpdname,
                               @"citycode":strcitycode,
                               @"brand":strbrand,
                               @"packageid":strpackageid,
                               @"institutionid":instiutionid,
                               @"emoney":stremoney}.mutableCopy
                     success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         blockSuccess(responseObject);
     }
                    failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         blockFailure(error);
     }];
    
}


// 3.7 众筹支付(微信、支付宝、银联)[包括发起人的支付和赞助人的支付]
- (void)requestFoundingPayitWithMid:(NSString *)strmid
                           password:(NSString *)strpassword
                               type:(NSString *)strtype
                               fdid:(NSString *)strfdid
                             donate:(NSString *)strdonate
                            success:(void (^)(NSDictionary *dictResponse))blockSuccess
                            failure:(void (^)(NSError *error))blockFailure{
    if (!(strmid && strpassword && strtype && strfdid && strdonate))
    {
        return;
    }
    
    [super getWithURLString:URL_FOUNDING_PAYIT
                     params:@{@"mid":strmid,
                              @"password":strpassword,
                              @"type":strtype,
                              @"fdid":strfdid,
                              @"donate":strdonate}.mutableCopy
                    success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         blockSuccess(responseObject);
     }
                    failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         blockFailure(error);
     }];
    
}

//3.8 获取项目分享到微信朋友圈的信息
- (void)requestFoundingGetShareWithMid:(NSString *)strmid
                              password:(NSString *)strpassword
                                  fdid:(NSString *)strfdid
                               success:(void (^)(NSDictionary *dictResponse))blockSuccess
                               failure:(void (^)(NSError *error))blockFailure{
    if (!(strmid && strpassword && strfdid))
    {
        return;
    }
    
    [super getWithURLString:URL_FOUNDING_GET_SHARE
                     params:@{@"mid":strmid,
                              @"password":strpassword,
                              @"fdid":strfdid}.mutableCopy
                    success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSDictionary *dict =(NSDictionary *)[responseObject objectForKey:@"content"];
         blockSuccess(dict);
     }
                    failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         blockFailure(error);
     }];
    
}

//4.1 通过条形码获取产品资料
- (void)requestProductGetInfoByBarcodeWithMid:(NSString *)strmid
                                      barcode:(NSString *)strbarcode
                                      success:(void (^)(NSDictionary *dictResponse))blockSuccess
                                      failure:(void (^)(NSError *error))blockFailure{
    if (!(strmid && strbarcode))
    {
        return;
    }
    
    [super getWithURLString:URL_PRODUCT_GET_INFO_BY_BARCODE
                     params:@{@"mid":strmid,                                             @"barcode":strbarcode}.mutableCopy
                    success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         blockSuccess(responseObject);
     }
                    failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         blockFailure(error);
     }];
    
}


//4.2 通过产品id获取产品信息
- (void)requestProductGetInfoByPidWithMid:(NSInteger)strmid
                                      pid:(NSInteger)strpid
                                  success:(void (^)(NSDictionary *dictResponse))blockSuccess
                                  failure:(void (^)(NSError *error))blockFailure{
    if (!(strmid && strpid))
    {
        return;
    }
    
    [super getWithURLString:URL_PRODUCT_GET_INFO_BY_PID
                     params:@{@"mid":@(strmid),
                              @"pid":@(strpid)}.mutableCopy
                    success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         blockSuccess(responseObject);
     }
                    failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         blockFailure(error);
     }];
    
}

//4.3 产品关键字搜索列表

- (void)requestProductGetInfoByKeywordsWithMid:(NSString *)strmid
                                      keywords:(NSString *)strkeywords
                                      keywords_py:(NSString *)strkeywords_py
                                       success:(void (^)(NSDictionary *dictResponse))blockSuccess
                                       failure:(void (^)(NSError *error))blockFailure{
    if (!(strmid && strkeywords))
    {
        return;
    }
    
    [super getWithURLString:URL_PRODUCT_GET_INFO_BY_KEYWORDS
                     params:@{@"mid":strmid,
                              @"keywords":strkeywords,
                              @"keywords_py":strkeywords_py}.mutableCopy
                    success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         blockSuccess(responseObject);
     }
                    failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         blockFailure(error);
     }];
    
}


//5.1 广告列表
- (void)requestIndexadlistsWithsuccess:(void (^)(NSDictionary *dictResponse))blockSuccess failure:(void (^)(NSError *error))blockFailure{
    
    [super getWithURLString:URL_INDEX_AD_LISTS
                     params:nil
                    success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         blockSuccess(responseObject);
     }
                    failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         blockFailure(error);
     }];
    
}


@end
